package example.controller;

//import org.patchca.color.SingleColorFactory;
//import org.patchca.filter.predefined.CurvesRippleFilterFactory;
//import org.patchca.font.RandomFontFactory;
//import org.patchca.service.ConfigurableCaptchaService;
//import org.patchca.utils.encoder.EncoderHelper;
//import org.patchca.word.RandomWordFactory;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/img")
public class PatchcaController {

    /**
     * 图片验证码
     *
     * @param request
     * @param response
     */
//    @RequestMapping("/createImg")
//    public void createImg(HttpServletRequest request, HttpServletResponse response) {
//        try {
//            ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
//            // new Color(25, 60, 170) 里面三个参数为红绿蓝三个颜色搭配出来的颜色
//            cs.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));
//            cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
//            // 验证码的长度
//            RandomWordFactory rwf = new RandomWordFactory();
//            rwf.setMinLength(4);
//            rwf.setMaxLength(4);
//            cs.setWordFactory(rwf);
//            // 设置框内字体的大小
//            RandomFontFactory ff = new RandomFontFactory();
//            ff.setMinSize(30);
//            ff.setMaxSize(30);
//            cs.setFontFactory(ff);
//            // 设置外框的大小
//            cs.setHeight(30);
//            cs.setWidth(100);
//            response.setContentType("image/png");
//            response.setHeader("Cache-Control", "no-cache, no-store");
//            response.setHeader("Pragma", "no-cache");
//            long time = System.currentTimeMillis();
//            response.setDateHeader("Last-Modified", time);
//            response.setDateHeader("Date", time);
//            response.setDateHeader("Expires", time);
//            ServletOutputStream stream = null;
//            stream = response.getOutputStream();
//            String validateCode = EncoderHelper.getChallangeAndWriteImage(cs, "png", stream);
//            System.out.println(validateCode);
//            stream.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 验证码
     *
     * @param width  图片宽度
     * @param height 图片高度
     * @param number 验证码数量
     */
//    @RequestMapping(value = {"/captcha"})
//    public void captcha(HttpServletRequest request,
//                        HttpServletResponse response, @RequestParam("w") int width, @RequestParam("h") int height, @RequestParam("n") int number,
//                        @RequestParam("a") int a, @RequestParam("b") int b, @RequestParam("c") int c
//    ) throws IOException {
//        ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
//        // new Color(25, 60, 170) 里面三个参数为红蓝绿三个颜色搭配出来的颜色
//        //cs.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));
//        cs.setColorFactory(new SingleColorFactory(new Color(a, b, c)));
//        cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
//        RandomFontFactory ff = new RandomFontFactory();
//        ff.setMinSize(30);
//        ff.setMaxSize(30);
//        RandomWordFactory rwf = new RandomWordFactory();
//        rwf.setMinLength(number);
//        rwf.setMaxLength(number);
//        cs.setWordFactory(rwf);
//        cs.setFontFactory(ff);
//        cs.setHeight(height);
//        cs.setWidth(width);
//        response.setContentType("image/png");
//        response.setHeader("Cache-Control", "no-cache, no-store");
//        response.setHeader("Pragma", "no-cache");
//        long time = System.currentTimeMillis();
//        response.setDateHeader("Last-Modified", time);
//        response.setDateHeader("Date", time);
//        response.setDateHeader("Expires", time);
//        ServletOutputStream stream = response.getOutputStream();
//        String validate_code = EncoderHelper.getChallangeAndWriteImage(cs, "png", stream);
//        stream.flush();
//        stream.close();
//    }
}
